/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

/*
 * PanelPreferencesConnection.java
 *
 * Created on Sep 11, 2011, 3:05:16 PM
 */
package group.pals.mailgate.preferences.ui;

import group.pals.mailgate.ui.IPreferences;
import group.pals.mailgate.ui.JSpinnerMouseWheelHandler;
import group.pals.mailgate.ui.Utils;
import group.pals.mailgate.preferences.Preferences;
import group.pals.utils.net.Proxy;
import group.pals.utils.sys.MessageBox;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Hai Bison
 */
public class PanelPreferencesConnection extends javax.swing.JPanel implements IPreferences {

    private final Preferences _Preferences;
    private final Utils UIUtils;

    /** Creates new form PanelPreferencesConnection */
    public PanelPreferencesConnection() {
        initComponents();

        this._Preferences = Preferences.getInstance();
        this.UIUtils = Utils.getInstance();

        initUI();
        initListeners();
    }

    private void initUI() {
        txtEmailCheckTimePeriod.setComponentPopupMenu(UIUtils.getEditorPopupMenu());
    }

    private void initListeners() {
        JSpinnerMouseWheelHandler mwh = JSpinnerMouseWheelHandler.getInstance();

        SpinnerNumberModel numberModel = new SpinnerNumberModel(
                Preferences.DefEmailCheckTimePeriod / 1000, 30, 60 * 60 * 24, 30);
        txtEmailCheckTimePeriod.setModel(numberModel);
        txtEmailCheckTimePeriod.addMouseWheelListener(mwh);

        numberModel = new SpinnerNumberModel(Preferences.DefConnectionTimeout / 1000, 5, 60 * 60 * 24, 30);
        txtConnectionTimeout.setModel(numberModel);
        txtConnectionTimeout.addMouseWheelListener(mwh);

        numberModel = new SpinnerNumberModel(0, 0, 65535, 1000);
        txtProxyPort.setModel(numberModel);
        txtProxyPort.addMouseWheelListener(mwh);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        txtEmailCheckTimePeriod = new javax.swing.JSpinner();
        txtConnectionTimeout = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        txtProxyHost = new javax.swing.JTextField();
        txtProxyUsername = new javax.swing.JTextField();
        txtProxyPort = new javax.swing.JSpinner();
        txtProxyPassword = new javax.swing.JPasswordField();
        chkUseProxy = new javax.swing.JCheckBox();

        setLayout(new java.awt.GridBagLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Threads:"));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        txtEmailCheckTimePeriod.setBorder(javax.swing.BorderFactory.createTitledBorder("Time period of mail check (seconds):"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(txtEmailCheckTimePeriod, gridBagConstraints);

        txtConnectionTimeout.setBorder(javax.swing.BorderFactory.createTitledBorder("Connection timeout (seconds):"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        jPanel2.add(txtConnectionTimeout, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel2, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Proxy:"));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        txtProxyHost.setBorder(javax.swing.BorderFactory.createTitledBorder("Host:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(txtProxyHost, gridBagConstraints);

        txtProxyUsername.setBorder(javax.swing.BorderFactory.createTitledBorder("Username:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(txtProxyUsername, gridBagConstraints);

        txtProxyPort.setBorder(javax.swing.BorderFactory.createTitledBorder("Port:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(txtProxyPort, gridBagConstraints);

        txtProxyPassword.setBorder(javax.swing.BorderFactory.createTitledBorder("Password:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(txtProxyPassword, gridBagConstraints);

        chkUseProxy.setFont(new java.awt.Font("Dialog", 0, 12));
        chkUseProxy.setText("Use SOCKS proxy");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(chkUseProxy, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        add(jPanel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chkUseProxy;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSpinner txtConnectionTimeout;
    private javax.swing.JSpinner txtEmailCheckTimePeriod;
    private javax.swing.JTextField txtProxyHost;
    private javax.swing.JPasswordField txtProxyPassword;
    private javax.swing.JSpinner txtProxyPort;
    private javax.swing.JTextField txtProxyUsername;
    // End of variables declaration//GEN-END:variables

    /*----------------------------------------------------------------------------
     * Utilities
     */
    /*----------------------------------------------------------------------------
     * IPreferences
     */
    public void loadPreferences() {
        txtEmailCheckTimePeriod.setValue(_Preferences.getEmailCheckTimePeriod() / 1000);
        txtConnectionTimeout.setValue(_Preferences.getConnectionTimeout() / 1000);

        chkUseProxy.setSelected(_Preferences.isUseSOCKSProxy());
        Proxy proxy = _Preferences.getSOCKSProxy();
        txtProxyHost.setText(proxy.getAddress());
        txtProxyPort.setValue(proxy.getPort());
        txtProxyUsername.setText(proxy.getUsername());
        txtProxyPassword.setText(proxy.getPassword());
    }

    public boolean storePreferences(boolean prepare) {
        if ((Integer) txtEmailCheckTimePeriod.getValue() <= 30) {
            MessageBox.showErrMsg(this, null, "Time period of mail check must be larger than 30 seconds");
            return false;
        }

        if ((Integer) txtConnectionTimeout.getValue() < 5) {
            MessageBox.showErrMsg(this, null, "Connection timeout must be larger or equal to 5 seconds");
            return false;
        }

        if (prepare) {
            return true;
        }

        _Preferences.setEmailCheckTimePeriod((Integer) txtEmailCheckTimePeriod.getValue() * 1000);
        _Preferences.setConnectionTimeout((Integer) txtConnectionTimeout.getValue() * 1000);

        _Preferences.setUseSOCKSProxy(chkUseProxy.isSelected());
        Proxy proxy = new Proxy(txtProxyHost.getText().trim(), (Integer) txtProxyPort.getValue());
        proxy.setUsername(txtProxyUsername.getText().trim()).setPassword(new String(txtProxyPassword.getPassword()).trim());
        _Preferences.setSOCKSProxy(proxy);

        return true;
    }

    public void closeResources() {
        //do nothing
    }
}
